package me.ele.spring.cloud.controller;

import me.ele.spring.cloud.client.DiscoveryZookeeperClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by enan on 16/8/31.
 */
@RestController
public class DiscoveryZookeeperClientController {

    @Autowired
    DiscoveryZookeeperClient discoveryZookeeperClient;

    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping("/")
    String index() {
        return discoveryZookeeperClient.index();
    }

    @RequestMapping("/all")
    String all() {
        System.out.println(discoveryClient.getServices());
        ServiceInstance serviceInstance0 = discoveryClient.getInstances("discovery-zookeeper").get(0);
        ServiceInstance serviceInstance1 = discoveryClient.getInstances("discovery-zookeeper").get(1);
        System.out.println(serviceInstance0.getHost() + ":" + serviceInstance0.getPort() + serviceInstance0.getUri() + "------" + serviceInstance0.getServiceId() + "------" + serviceInstance0.getMetadata().toString());
        System.out.println(serviceInstance1.getHost() + ":" + serviceInstance1.getPort() + serviceInstance1.getUri() + "------" + serviceInstance1.getServiceId() + "------" + serviceInstance1.getMetadata().toString());
        return "all";
    }
}
